import axios from "axios";
import store from "@/store"
//引入进度条
import nprogress from 'nprogress';
//start：进度条开始 done：进度条结束
import "nprogress/nprogress.css"

let chatReq = axios.create({
  baseURL: "localhost:5000/webhooks",
  // 允许跨域
  changeOrigin: true,
  ws: true,
});


//请求拦截器
chatReq.interceptors.request.use((config) => {
  nprogress.start();
  //需要携带token带给服务器
  if (store.state.user.token){
    config.headers.token = store.state.user.token
  }
  //config：配置对象，里面有一个属性为headers请求头
  return config;
})

//响应拦截器
chatReq.interceptors.response.use((res)=>{
  //进度条结束
  nprogress.done();
  //成功回调函数
  return res.data;
},()=>{
  //失败回调函数
  alert("服务器响应数据失败");
})

export default chatReq;
